<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/19
 * Time: 15:11
 */

namespace Api\Behavior;
use Admin\Api\DatatreeApi;
use Shop\Api\RedEnvelopeApi;
use Shop\Model\RedEnvelopeModel;


/**
 * 添加红包
 * Class MessageSendBehavior
 * @package Api\Behavior
 */
class RedEnvelopeBehavior  extends \Think\Behavior{

    /**
     * 传入6个参数
     * 1. uid
     * 2. type
     * 3. money
     * 4. use_condition
     * 5. notes
     * 6. expire_time
     * @param mixed $params
     */
    public function run(&$params)
    {
        addLog("RedEnvelopeBehavior", $params, "", "[调试]调用红包接口");

        $uid = $params['uid'];//拥有、获得红包的人的UID
        $type= $params['type']; //红包类别
        $money =  floatval($params['money']); //红包可抵扣金额
        $use_condition = floatval($params['use_condition']);//红包满$condition元 才可使用
        $notes = $params['notes']; //备注说明
        $expire_time = $params['expire_time']; //红包过期时间

        $result  = apiCall(DatatreeApi::GET_INFO,array(array('id'=>$type)));

        if(!$result['status'] || is_null($result['info'])){
            addLog("RedEnvelopeBehavior", $params, "红包类别无效", "[调试]调用红包接口");
            return ;
        }

        if($money < 0){
            $money  = 0;
        }

        if($use_condition < 0){
            $use_condition = 0;
        }

        $entity = array(
            'uid'=>$uid,
            'money'=>$money,
            'notes'=>$notes,
            'dtree_type'=>$type,
            'use_condition'=>$use_condition,
            'use_status'=>RedEnvelopeModel::NOT_USE,
            'expire_time'=>$expire_time,
            'get_time'=>time(),

        );

        $result = apiCall(RedEnvelopeApi::ADD,array($entity));


        $params['result'] = $result['status'];
    }

}